package file

import (
    "bufio"
    "io"
    "os"
)

// GetContent 获取文件内容
func GetContent(path string) []string {
    file, err := os.OpenFile(path, os.O_RDONLY, 0666)
    defer file.Close()
    if err != nil {
        panic(err)
    }
    reader := bufio.NewReader(file)
    var content []string
    for {
        str, err := reader.ReadString('\n')
        content = append(content, str)
        if err == io.EOF {
            break
        }
    }
    return content
}

// PutContent 追加写入
func PutContent(path string, content string) bool {
    file, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
    defer file.Close()
    if err != nil {
        return false
    }
    writer := bufio.NewWriter(file)
    rows, err := writer.WriteString(content + "\r\n")
    if err != nil {
        return false
    }
    writer.Flush()
    return rows > 0
}
